/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dao.GradeMapper;
import org.xstgongyi.eduaid.dataobject.GradeDO;

import java.util.List;

/**
 * manager层代码，作为module的内部代码，不应该被module外部调用，也不应该被service之上各层调用。 调用者需要保证参数的合法性。
 *
 * @author oldcrane
 * @created 2019-04-24
 */
@Service
@CacheConfig(cacheNames = "grade")
public class GradeManager {

    @Autowired
    private GradeMapper dao;

    @CacheEvict(allEntries = true)
    public void insert(GradeDO grade) {
        dao.insert(grade);
    }

    @CacheEvict(allEntries = true)
    public void update(GradeDO grade) {
        dao.update(grade);
    }

    @Cacheable(key = "'list-all'")
    public List<GradeDO> listAll() {
        return dao.listAll();
    }

    @Cacheable(key = "'list-status-' + #active")
    public List<GradeDO> listByStatus(Boolean active) {
        return dao.listAllByStatus(active);
    }

    /**
     * 调用者需确保参数不为null
     */
    @Cacheable(key = "#id")
    public GradeDO getById(Integer id) {
        return dao.getById(id);
    }

}
